<?php

namespace app\models\search;

use app\models\ThingRecordFile;
use yii\base\Model;
use yii\data\ActiveDataProvider;

/**
 * ThingRecordFileSearchModel represents the model behind the search form of `app\models\ThingRecordFile`.
 */
class ThingRecordFileSearchModel extends ThingRecordFile
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['ID', 'TallWay'], 'integer'],
            [['Caller', 'EmployeeNumber', 'Agent', 'TallArea'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = ThingRecordFile::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'ID' => $this->ID,
            'TallTimeStart' => $this->TallTimeStart,
            'TallTimeEnd' => $this->TallTimeEnd,
            'ZxCh' => $this->ZxCh,
            'TrunkCh' => $this->TrunkCh,
            'TallWay' => $this->TallWay,
            'Delflag' => $this->Delflag,
            'Flag' => $this->Flag,
        ]);

        $query->andFilterWhere(['like', 'Caller', $this->Caller])
            ->andFilterWhere(['like', 'TrunkCaller', $this->TrunkCaller])
            ->andFilterWhere(['like', 'EmployeeNumber', $this->EmployeeNumber])
            ->andFilterWhere(['like', 'Agent', $this->Agent])
            ->andFilterWhere(['like', 'Filepath', $this->Filepath])
            ->andFilterWhere(['like', 'ShareFilePath', $this->ShareFilePath])
            ->andFilterWhere(['like', 'HttpFilePath', $this->HttpFilePath])
            ->andFilterWhere(['like', 'BrowseMan', $this->BrowseMan])
            ->andFilterWhere(['like', 'Memo', $this->Memo])
            ->andFilterWhere(['like', 'TallArea', $this->TallArea])
            ->andFilterWhere(['like', 'OperNumber', $this->OperNumber])
            ->andFilterWhere(['like', 'BrowseMans', $this->BrowseMans]);
//            ->andFilterWhere(['like', 'OperID', $this->OperID])
//             ->andFilterWhere(['between', 'TallTimeStart', 
//                 date('Y-m-d H:i:s',strtotime($startDate)),
//                 date('Y-m-d H:i:s',strtotime($endDate))]);
        //根据时间筛选记录的列表
        if(isset($_GET['timeZones'])){
            $timeZones = $_GET['timeZones'];
            $startDate = trim(explode('-',$timeZones)[0]);
            $startDate = str_replace('年', '-', $startDate);
            $startDate = str_replace('月', '-', $startDate);
            $startDate = str_replace('日', ' ', $startDate);
            $endDate = trim(explode('-',$timeZones)[1]);
            $endDate = str_replace('年', '-', $endDate);
            $endDate = str_replace('月', '-', $endDate);
            $endDate = str_replace('日', ' ', $endDate);
            $query->andFilterWhere(['between', 'TallTimeStart',
                date('Y-m-d H:i:s',strtotime($startDate)),
                date('Y-m-d H:i:s',strtotime($endDate))
            ]);
        }

        if(isset($_GET['trainTimeZones'])){
            $trainTimeZones = $_GET['trainTimeZones'];
            $startDate = trim(explode('-',$trainTimeZones)[0]);
            $startDate = str_replace('年', '-', $startDate);
            $startDate = str_replace('月', '-', $startDate);
            $startDate = str_replace('日', ' ', $startDate);
            $endDate = trim(explode('-',$trainTimeZones)[1]);
            $endDate = str_replace('年', '-', $endDate);
            $endDate = str_replace('月', '-', $endDate);
            $endDate = str_replace('日', ' ', $endDate);
//            $query->andFilterWhere(['between', 'TallTimeStart',
//                date('Y-m-d H:i:s',strtotime($startDate)),
//                date('Y-m-d H:i:s',strtotime($endDate))
//            ]);
            $query->andFilterWhere(['between', 'create_time',
                date('Y-m-d H:i:s',strtotime($startDate)),
                date('Y-m-d H:i:s',strtotime($endDate))
            ]);
        }
//        $sql = "set ANSI_NULLS on|
//            set ANSI_WARNINGS on|
//            select * from OPENQUERY(mysqlCreate,'select * from arc_progress') as l join dbo.Thing_RecordFile as t on l.thing_recordfile=t.ID";
//        $this->getArcProgress()->one();
//        $array = explode("|",$sql);
//        $connection  = $this->getDb();
//        foreach ($array as $v){
//            $command = $connection->createCommand($v);
//            $res     = $command->queryAll();
//            var_dump($res);
//        }
        return $dataProvider;
    }
}
